from fastapi import APIRouter, UploadFile, File, Depends, HTTPException
from sqlalchemy.orm import Session
from app.services.file import FileService
from app.db.session import get_db
from app.utils.security import get_current_user
from app.db.models.user import User
from app.core.config import settings  # 导入配置

router = APIRouter(prefix="/file", tags=["文件管理"])

@router.post("/upload/image", summary="上传图片")
async def upload_image(
    file: UploadFile = File(...),
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    if not file.content_type.startswith('image/'):
        raise HTTPException(status_code=400, detail="只允许上传图片文件")
    url = await FileService.upload_image(file, current_user.id)
    # 使用配置中的七牛云域名
    if not url.startswith("http"):
        url = f"https://{settings.QINIU_DOMAIN}{url.replace('/static', '')}"
    return {"code": 0, "message": "成功！", "data": url}

@router.post("/upload/file", summary="上传文件")
async def upload_file(
    file: UploadFile = File(...),
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    url = await FileService.upload_file(file, current_user.id)
    if not url.startswith("http"):
        url = f"https://{settings.QINIU_DOMAIN}{url.replace('/static', '')}"
    return {"code": 0, "message": "成功！", "data": url} 